<template>
	<view>
		<view class="sticky-box">
			<search-input ref="searchInput" placeholder="扫处方码或输入手机姓名查询" rightText="清空" :isRight="true"
				@searchConfirm="handleSearch($event)" @searchRight="handerRight">
			</search-input>
		</view>
		<view class="search-nav flex-row">
			<view class="status-show" :class="{active:item.id==status}" v-for="(item,index) in statusList" :key="index"
				@tap="selectNav(item)">{{item.name}}</view>
		</view>
		<view class="goods-list">
			<view class="goods-item" @tap="toGoodsDetail(item)" v-for="(item, index) in goodsList" :key="index">
				<view class="code-status flex-row">
					<view class="prescription-code">
						<text>{{item.Code}}</text>
					</view>

					<view class="prescription-status">
						<text>{{item.RecieveStatus}}</text>
					</view>
				</view>
				<view class="flex-row">
					<view class="goods-name">
						<text style="font-weight: bold;">{{item.PatientName}} | {{item.PatientIdCode}} | {{item.Mobile}} </text>
					</view>
				</view>
				<view class="flex-row">
					<view class="address">
						<text style="font-weight: 400;">{{item.PatientAddress}} </text>
					</view>
				</view>
				<view class="goods-panel" v-for="(good,gIndex) in item.PharmacyStockOutDetailList">
					<view class="goods-info flex-row">
						<view class="info-name"> {{good.Name}}</view></text>
						<view style="color:#5d71cb"> {{good.Price}}</view></text>
					</view>
					<view class="goods-info flex-row">
						<view class=""> {{good.Manufacturer}}</view></text>
						<view class=""> {{good.ApprovalNumber}}</view></text>
					</view>
					<view class="goods-info flex-row">
						<view class=""> {{good.PackageSpec}}</view></text>
						<view class="goods-number">
							<text style="color: red;">*{{good.Amount}}</text>
						</view>
					</view>


				</view>
				<view class="flex-row hospital-show">
					<view>
						<text>{{item.InstitutionName}}</text>
					</view>
					<view>
						<text>{{item.UpdateTime}}</text>
					</view>
				</view>
				<!-- <view class="btns flex-row">
					<view class="btn resume">拒签</view>
					<view class="btn accept">签收</view>
				</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import searchInput from '@/components/search-input/search-input.vue'
	var that;
	export default {
		components: {
			searchInput
		},
		data() {
			return {
				searchVal: '',
				SupplierCode: '',
				Code: '',
				statusList: [{
					id: "",
					name: '全部'
				}, {
					id: "0",
					name: '待配送'
				}, {
					id: "1",
					name: '已配送'
				}, {
					id: "-1",
					name: '拒签'
				}],
				status: "",
				goodsList: [],
				goodsTempList: [],
				pageSize: 5000,
				pageNum: 1,
				currentNav: 15
			}
		},
		watch: {
			'$route': 'getServerData'
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
				companyGoods: state => state.companyGoods,
			})
		},
		onLoad() {
			that = this
			const title = that.$Route.query.Send_Ent_Name ?? '';
			that.SupplierCode = that.$Route.query.SupplierCode ?? '';

			uni.setNavigationBarTitle({
				title: title
			})
		},
		onShow() {
			that.getServerData()
		},
		onReady() {
			that.$refs.searchInput.setfocus();
		},
		onUnload() {
			that.cleanScanCode()
		},

		onReachBottom: function() {
			let pageCount = 0;
			if (that.goodsTempList.length % 100 == 0) {
				pageCount = Math.floor(that.goodsTempList.length / 100)
			} else {
				pageCount = Math.floor(that.goodsTempList.length / 100) + 1
			}

			if (that.pageNum < pageCount) {
				that.pageNum += 1;
				that.getShowPageList(that.pageNum);
			}
		},
		methods: {
			...mapMutations(['initStockInGoods', 'initScanCode', 'cleanScanCode', 'initCompanyGoods', 'initCompanyGood']),
			getServerData() {
				that.getGoodsList()
			},
			selectNav(item) {
				that.status = item.id;
				that.pageNum = 1;
				that.getGoodsList();
			},
			getShowPageList(page) {
				const start = (page - 1) * 100;
				if (page == 1) {
					that.goodsList = that.goodsTempList.slice(start, start + 100);
				} else {
					let list = that.goodsTempList.slice(start, start + 100);
					console.log(list)
					that.goodsList = that.goodsList.concat(list);
				}

			},
			//查询处方单号
			getGoodsList() {
				//console.log(' 更新了')				
				let query = {
					PrescriptionCode: '',
					keyword: '',
					RecieveStatus: this.status,
					DateSpan: that.currentNav
				}
				let requestData = {
					pagination: {
						rows: that.pageSize,
						page: that.pageNum
					},
					queryJson: JSON.stringify(query)
				};
				uni.showLoading({
					title: '加载中'
				});
				//console.log(requestData)
				that.$request.globalRequest('supplierstock/wplist', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading();
					console.log(res)
					let data = res;
					if (data) {
						that.goodsTempList = data.map(item=>{
							var newItem = item;
							newItem.PharmacyStockOutDetailList = item.PharmacyStockOutDetailList.map(p=>{
								var newP = p;
								newP.scan = 0;
								newP.noscan = p.Amount;
								newP.scanCodeList = []
								return newP
							})
							return newItem;
						});
						//缓存供应商所有的处方						
						that.initCompanyGoods(that.goodsTempList)
						that.getShowPageList(1);

					} else {
						that.goodsList = [];
					}
					that.$refs.searchInput.cleanInput();
				}).catch(res => {
					uni.hideLoading();
					that.$common.showNormalToast(res);
				})
			},
			handerRight() {
				var companyGoods = uni.getStorageSync('companyGoodsList') || [];
				that.goodsList = companyGoods;
			},
			handleSearch(res) {
				that.searchVal = res.value;
				that.getFilterGoodsList(that.searchVal);
			},
			//过滤查询
			getFilterGoodsList(value) {
				let companyGoods = uni.getStorageSync('companyGoodsList') || [];
				//用当前溯源码查询
				var filterGoods = companyGoods.filter(good => good.Code.indexOf(value) != -1 || good.PatientName.indexOf(
					value) != -1);
				if (filterGoods.length == 1) {
					//如果只匹配一个商品，则直接跳转到详情页
					that.initCompanyGood(filterGoods[0]);
					let url = {
						path: '/pagesStockIn/goodsDetail/goodsDetail?transCode'
					}
					that.$Router.push(url)
				} else if (filterGoods.length == 0) {
					that.$common.showNormalToast("未匹配到记录");
					that.$refs.searchInput.cleanInput();
				} else {
					that.$common.showNormalToast("匹配到" + filterGoods.length + "条记录");
					that.goodsList = filterGoods;
					that.$refs.searchInput.cleanInput();
				}

			},
			toGoodsDetail(item) {
				//设置当前正在扫码的商品
				that.initCompanyGood(item);
				let url = {
					path: '/pagesStockIn/goodsDetail/goodsDetail'
				}
				that.$refs.searchInput.cleanInput();
				that.$Router.push(url)
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "goodsList.scss";
</style>